package com.space.school.controller;

import com.space.school.dao.UserInsert;
import com.space.school.dao.impl.UserInsertImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UserInsertServlet",value = "/UserInsertServlet")
public class UserInsertServlet extends HttpServlet {
    UserInsert userInsert=new UserInsertImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("insertUserList.jsp").forward(req,resp);
    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("userName");
        String password = req.getParameter("password");
        int flag=userInsert.insert(username,password);
        if(flag==3){
            resp.sendRedirect("/userServlet");
        }else if(flag==1){
            req.getSession().setAttribute("insert_message","你的账户已存在");
            req.getRequestDispatcher("insertUserList.jsp").forward(req,resp);
        }
        else if(flag==2){
            req.getSession().setAttribute("insert_message","你的密码设置太简单了,请重新注册");
            req.getRequestDispatcher("register.jsp").forward(req,resp);
        }
    }
}
